import wepy from 'wepy'
/**
 * @description 组件的基本方法,该方法为所有业务组件 (module) 的基础类
 *      给mixin 该类的组件 提供   onVisiable 方法   visiable 属性代表是否显示
 *      onVisiable(first,expired)  first 是否第一次调用 expired 代表设置cache 后，调用onVisiable超过期限
 * @author 简文彬 2018-10-12
 * 
 */
export default class CommonModuleMixin extends wepy.mixin {
    props = {
        //tab组件显示顺序
        tabIndex: { type: Number, twoWay: true },
        //数据缓存分钟,
        cache: { type: Number, default: 0 },
        setting: { type: Object },
        TAG: { type: String }
    }
    data = {
        //是否加载过
        loaded: false,
        visiable: false,
        last_load_time: 0,
    }
    computed = {
        option() {
            if (this.TAG)
                return this.setting[this.TAG];
            return null;
        }
    }
    watch = {
        tabIndex(newValue, oldVlue) {
            //console.log(newValue);
            if (this.option && newValue === this.option.tabIndex) {
                this.visiable = true;
                this._onLoad();
            }
            else {
                this.visiable = false;
                this.$apply();
            }
        }
    }
    _onLoad() {
        if (this.onVisiable) {
            let expired = new Date().getTime() < this.last_load_time + this.cache * 1000;
            this.onVisiable(!this.loaded, expired);
            this.last_load_time = new Date().getTime();
        }
        if (!this.loaded) {
            this.loaded = true;
        }
        this.$apply();
    }
    onLoad() {
        if (this.option && this.tabIndex === this.option.tabIndex)
            this.visiable = true;
        //console.log(this.option);
        //console.log(this.tabIndex);
        //console.log('onLoad:' + this.visiable);
        console.log(this.TAG);
        if (this.visiable) {
            this._onLoad();
        }
    }
    onShow() {
        //console.log('onShow' + this.visiable);
        if (this.visiable) {
            this._onLoad();
        }
    }
}